package com.pb.camera.more.detail;

import com.pb.camera.R;
import com.pb.camera.manager.EventManager;
import com.pb.camera.utils.TaskHelper;
import com.pb.camera.view.MyCheckBox;
import com.pb.camera.work.ControlTask;
import com.tutk.IOTC.AVIOCTRLDEFs;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

import java.util.Observable;

public class MoveDetectSetFragment extends BaseSetFragment implements OnClickListener {
    private View mRootView;
    private MyCheckBox mCBMoveDetect;
    private MyCheckBox mCBMoveSendWarn;

    private MyCheckBox mCBRecoder;
    private MyCheckBox mCBRecordDetect;
    private MyCheckBox mCBRecordFullTime;
    private TextView mTVSensitive;
    private MyCheckedListener mMyCheckedListener;
    private int mDetectSensitive;
    private boolean mIsMoveDetect;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_move_detect_set, container, false);
        super.onCreateView(inflater, container, savedInstanceState);
        return mRootView;
    }

    @Override
    protected void initListener() {
        mRootView.findViewById(R.id.lv_record_sensitive).setOnClickListener(this);
        mMyCheckedListener = new MyCheckedListener();
        mCBMoveDetect.setOnCheckedChangeListener(mMyCheckedListener);
        mCBRecordFullTime.setOnCheckedChangeListener(mMyCheckedListener);
        mCBRecordDetect.setOnCheckedChangeListener(mMyCheckedListener);
        mCBRecoder.setOnCheckedChangeListener(mMyCheckedListener);
    }

    @Override
    public void initData() {
        if (isInited)
            return;
        progressDialog.show();
        //TODO: 取数据，初始化，注意相对布局的隐藏
        TaskHelper.exec(new TaskHelper.Task() {
            @Override
            public void execute() throws Exception {
                ControlTask.getMotionDetect(0);
                ControlTask.getRecordMode(0);
            }

            @Override
            public void callback(Exception e) {
            }
        });
    }

    @Override
    protected void initView() {
        mCBMoveDetect = (MyCheckBox) mRootView.findViewById(R.id.cb_move_detect);
        mCBMoveSendWarn = (MyCheckBox) mRootView.findViewById(R.id.cb_send_warning);
        mTVSensitive = (TextView) mRootView.findViewById(R.id.tv_sensitive_level);
        mCBRecoder = (MyCheckBox) mRootView.findViewById(R.id.cb_record);
        mCBRecordDetect = (MyCheckBox) mRootView.findViewById(R.id.rb_move_detect_recoeder);
        mCBRecordFullTime = (MyCheckBox) mRootView.findViewById(R.id.rb_full_time_recoeder);
    }

    @Override
    public void update(Observable observable, Object data) {
        if (data instanceof EventManager.InitFallEvent && isIniting)
            initData();
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlGetMotionDetectResp) {
            progressDialog.dismiss();
            if (((AVIOCTRLDEFs.SMsgAVIoctrlGetMotionDetectResp) data).sensitivity > 0) {
                mCBMoveDetect.setUnListenChecked(true);
                mDetectSensitive = ((AVIOCTRLDEFs.SMsgAVIoctrlGetMotionDetectResp) data).sensitivity;
                setSensitiveText();
            }
            setIsInited(true);
        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlSetMotionDetectResp) {
            progressDialog.dismiss();
            if (((AVIOCTRLDEFs.SMsgAVIoctrlSetMotionDetectResp) data).result != 0)
                Toast.makeText(getActivity(), R.string.set_failed, Toast.LENGTH_SHORT).show();
        }
        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlSetRecordResp) {
            progressDialog.dismiss();
            if (((AVIOCTRLDEFs.SMsgAVIoctrlSetRecordResp) data).result != 0)
                Toast.makeText(getActivity(), R.string.set_failed, Toast.LENGTH_SHORT).show();
        }


        if (data instanceof AVIOCTRLDEFs.SMsgAVIoctrlGetRecordResp) {
            progressDialog.dismiss();
            if (((AVIOCTRLDEFs.SMsgAVIoctrlGetRecordResp) data).recordType == AVIOCTRLDEFs.AVIOTC_RECORDTYPE_FULLTIME) {

                mCBRecordFullTime.setUnListenChecked(true);

                mCBRecoder.setUnListenChecked(true);
            }
            if (((AVIOCTRLDEFs.SMsgAVIoctrlGetRecordResp) data).recordType == AVIOCTRLDEFs.AVIOTC_RECORDTYPE_ALAM) {

                mCBRecordDetect.setUnListenChecked(true);

                mCBRecoder.setUnListenChecked(true);
            }
            setIsInited(true);
        }
    }

    private void setSensitiveText() {
        switch (mDetectSensitive / 25) {
            case 0:
                mTVSensitive.setText(R.string.low);
                break;
            case 1:
                mTVSensitive.setText(R.string.middle);
                break;
            case 2:
                mTVSensitive.setText(R.string.high);
                break;
            case 3:
                mTVSensitive.setText(R.string.most_high);
                break;
        }
    }

    class MyCheckedListener implements OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     final boolean isChecked) {
            switch (buttonView.getId()) {
                case R.id.cb_move_detect: {
                    mIsMoveDetect = isChecked;
                    progressDialog.show();
                    TaskHelper.exec(new TaskHelper.Task() {
                        @Override
                        public void execute() throws Exception {
                            progressDialog.show();
                            if (isChecked) {
                                mDetectSensitive = mDetectSensitive == 0 ? 49 : mDetectSensitive;
                                ControlTask.setMotionDetect(0, mDetectSensitive);
                            } else {
                                ControlTask.setMotionDetect(0, 0);
                                mDetectSensitive = 0;
                            }
                        }

                        @Override
                        public void callback(Exception e) {
                            if (!isChecked) {

                                mCBMoveSendWarn.setUnListenChecked(false);
                                mCBMoveSendWarn.setEnabled(false);
                                mTVSensitive.setText(R.string.close);
                            } else {
                                setSensitiveText();
                                mCBMoveSendWarn.setEnabled(true);
                            }
                        }
                    });
                }
                break;
                case R.id.cb_send_warning:
                    progressDialog.show();

                    break;
                case R.id.rb_move_detect_recoeder:
                    progressDialog.show();
                    if (isChecked) {
                        progressDialog.show();
                        mCBRecordFullTime.setUnListenChecked(false);
                        TaskHelper.exec(new TaskHelper.Task() {
                            @Override
                            public void execute() throws Exception {
                                ControlTask.setRecordMode(0, AVIOCTRLDEFs.AVIOTC_RECORDTYPE_ALAM);
                            }

                            @Override
                            public void callback(Exception e) {
                            }
                        });
                    }
                    break;
                case R.id.rb_full_time_recoeder:
                    progressDialog.show();
                    if (isChecked) {
                        mCBRecordDetect.setUnListenChecked(false);
                        TaskHelper.exec(new TaskHelper.Task() {
                            @Override
                            public void execute() throws Exception {
                                ControlTask.setRecordMode(0, AVIOCTRLDEFs.AVIOTC_RECORDTYPE_FULLTIME);
                            }

                            @Override
                            public void callback(Exception e) {
                            }
                        });
                    }
                    break;
                case R.id.cb_record: {
                    TaskHelper.exec(new TaskHelper.Task() {
                        @Override
                        public void execute() throws Exception {
                            if (isChecked)
                                ControlTask.setRecordMode(0, AVIOCTRLDEFs.AVIOTC_RECORDTYPE_ALAM);
                            else
                                ControlTask.setRecordMode(0, AVIOCTRLDEFs.AVIOTC_RECORDTYPE_OFF);
                        }

                        @Override
                        public void callback(Exception e) {
                        }
                    });
                    if (!isChecked) {
                        mCBRecordDetect.setUnListenChecked(false);
                        mCBRecordDetect.setEnabled(false);
                        mCBRecordFullTime.setUnListenChecked(false);
                        mCBRecordFullTime.setEnabled(false);
                    } else {
                        mCBRecordFullTime.setUnListenChecked(true);
                        if (!mCBRecordDetect.isEnabled())
                            mCBRecordDetect.setEnabled(true);
                        if (!mCBRecordFullTime.isEnabled())
                            mCBRecordFullTime.setEnabled(true);
                    }
                }
                break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.lv_record_sensitive: {
                if (!mCBRecoder.isChecked())
                    return;
                View layoutView = View.inflate(getActivity(), R.layout.select_sensitive_dialog, null);
                layoutView.findViewById(R.id.rb_detect_record_low).setOnClickListener(this);
                layoutView.findViewById(R.id.rb_detect_record_middle).setOnClickListener(this);
                layoutView.findViewById(R.id.rb_detect_record_high).setOnClickListener(this);
                layoutView.findViewById(R.id.rb_detect_record_most_high).setOnClickListener(this);
                new AlertDialog.Builder(getActivity(), R.style.theme_dialog_alert)
                        .setView(layoutView)
                        .setPositiveButton(R.string.button_positive, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mTVSensitive.setText(mSensitivie);
                                TaskHelper.exec(new TaskHelper.Task() {
                                    @Override
                                    public void execute() throws Exception {
                                        ControlTask.setRecordMode(0, mDetectSensitive);
                                    }

                                    @Override
                                    public void callback(Exception e) {
                                    }
                                });
                            }
                        })
                        .setNegativeButton(R.string.button_negative, null).show();
            }
            break;
            case R.id.rb_detect_record_low:
                mDetectSensitive = 24;
                getTextLevel(v);
                break;
            case R.id.rb_detect_record_middle:
                mDetectSensitive = 49;
                getTextLevel(v);
                break;
            case R.id.rb_detect_record_high:
                mDetectSensitive = 74;
                getTextLevel(v);
                break;
            case R.id.rb_detect_record_most_high:
                mDetectSensitive = 99;
                getTextLevel(v);
                break;
            default:
                break;
        }
    }

    private String mSensitivie;

    private void getTextLevel(View view) {
        mSensitivie = ((RadioButton) view).getText().toString();
    }
}
